package com.example.speeddating.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.speeddating.entity.Preference;
import com.example.speeddating.enums.ResponseCodeEnum;
import com.example.speeddating.exception.CommonException;
import com.example.speeddating.mapper.PreferenceMapper;
import com.example.speeddating.mapper.UserMapper;
import com.example.speeddating.service.PreferenceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author tina
 * @since 2023-08-05 03:18:43
 */
@Service
public class PreferenceServiceImpl extends ServiceImpl<PreferenceMapper, Preference> implements PreferenceService {

    @Autowired
    private PreferenceMapper preferenceMapper;

    @Override
    public void updatePreference(Preference preference) {
        QueryWrapper<Preference> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",preference.getUserId());
        Preference preference1 = preferenceMapper.selectOne(queryWrapper);
        if (preference1!=null){
            preference.setId(preference1.getId());
            preferenceMapper.updateById(preference);
        }else {
            preferenceMapper.insert(preference);
        }
    }

    @Override
    public Preference selectPreferenceByUserId(Long id) {
        QueryWrapper<Preference> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        Preference p = preferenceMapper.selectOne(queryWrapper);
        if (p!=null){
            return p;
        }else {
            Preference preference = new Preference();
            preference.setUserId(id);
            preference.setEducation("本科");
            preference.setMaxAge(2000);
            preference.setMinAge(1970);
            preference.setMaxHeight(180d);
            preference.setMinHeight(160d);
            preference.setCityType(0);  //默认不限城市
            preferenceMapper.insert(preference);
            return preference;
        }
    }

    @Override
    public void resetPreference(Long id) {
        QueryWrapper<Preference> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        Preference preference = preferenceMapper.selectOne(queryWrapper);
        if (preference==null){
            throw new CommonException(ResponseCodeEnum.CODE_603);
        }
        Preference p = new Preference();
        p.setId(preference.getId());
        preference.setEducation("本科");
        preference.setMaxAge(2000);
        preference.setMinAge(1970);
        preference.setMaxHeight(180d);
        preference.setMinHeight(160d);
        preference.setCityType(0);  //默认不限城市
        preferenceMapper.updateById(preference);
    }
}
